package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.api.service.Gwat171Service;
import cn.e4j.modular.api.service.GwbService;
import cn.e4j.modular.crm.entity.GwbhT;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商机更改审批 控制类
 * Created by yuhuan on 2018/7/13.
 */
@RestController
@RequestMapping("api/wx/gwat171")
public class Gwat171Controller {
    @Autowired
    Gwat171Service gwat171Service;

    @Autowired
    GwbService gwbService;

    @Autowired
    SystemServiceImpl systemService;

    /**
     * 获取拜访记录审批信息
     *
     * @return
     */
    @RequestMapping(value = "getGwbhData")
    public ResponseData getGwbhData(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        int errcode = 200;
        String errmsg = "ok";
        List<Dto> list = new ArrayList<Dto>();
        try {
            inDto.put("USERID", currentUser.getID());

            //客户权限
            Dto gwb001 = gwbService.getGwb001(currentUser, inDto);
            inDto.put("CUSTOMERS", gwb001.get("CUSTOMERS"));

            //上下级权限
            Dto sys003 = systemService.getSys003(currentUser);
            inDto.put("ORGS", sys003.get("ORGS"));

            //业务员权限
            Dto sys004 = systemService.getSys004(currentUser, (List<Dto>) sys003.get("ORGS"));
            inDto.put("YWY", sys004.get("YWY"));

            list = InvokeUtil.invokeBeanMethod("gwat171Mapper,getGwbhListPage",inDto);
        } catch (Exception e) {
            e.printStackTrace();
            errcode = 500;
            errmsg = "服务器出小差了";
            return new ErrorResponseData(errcode, errmsg);
        }

        return new SuccessResponseData(list);
    }

    /**
     * 确认、取消确认  update
     *
     * @return
     */
    @RequestMapping("updateByKey")
    public ResponseData updateByKey(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        JSONObject inJson = new JSONObject();
        JSONArray characteristics = new JSONArray();
        SysUser currentUser = wapper.getSysUser();
        Date date = new Date();
        int errcode = 200;
        String errmsg = "ok";
        try {
            inJson = JSONObject.parseObject(inDto.getString("obj"));
            characteristics = inJson.getJSONArray("CHARACTERISTICS");
            for (int i = 0; i < characteristics.size(); i++) {
                JSONObject item = characteristics.getJSONObject(i);
                GwbhT gwbh_tPO = new GwbhT();
                gwbh_tPO.setGWBHENT(currentUser.getDBC());
                gwbh_tPO.setGWBHACTI(item.getString("GWBHACTI"));
                gwbh_tPO.setGWBHNAME(item.getString("GWBHNAME"));
                gwbh_tPO.setGWBHID(item.getString("GWBHID"));
                gwbh_tPO.setGWBHSEQ(item.getBigDecimal("GWBHSEQ"));
                gwbh_tPO.setGWBHUSER(currentUser.getID());
                gwbh_tPO.setGWBHGRUP(currentUser.getORG_ID());
                gwbh_tPO.setGWBHORIU(currentUser.getID());
                //gwbh_tPO.setGwbhorig(currentUser.getOrg_id_());
                //gwbh_tPO.setGWBHDATE(DateUtils.parseDate(DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd)));
                gwbh_tPO.setGWBHDATE(LocalDate.now());
                gwbh_tPO.setGWBHTIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));
                UpdateWrapper<GwbhT> gwbhTUpdateWrapper = new UpdateWrapper<>();
                gwbhTUpdateWrapper.eq("GWBHENT", gwbh_tPO.getGWBHENT());
                gwbhTUpdateWrapper.eq("GWBHNAME", gwbh_tPO.getGWBHNAME());
                gwbhTUpdateWrapper.eq("GWBHID", gwbh_tPO.getGWBHID());
                gwbhTUpdateWrapper.eq("GWBHSEQ", gwbh_tPO.getGWBHSEQ());
                gwbh_tPO.update(gwbhTUpdateWrapper);
            }
        } catch (Exception e) {
            e.printStackTrace();
            errcode = 500;
            errmsg = "服务器出小差了";
            return new ErrorResponseData(errcode, errmsg);
        }

        return new SuccessResponseData();
    }

    /**
     * 获取详细信息
     *
     * @return
     */
    @RequestMapping(value = "getGwbhInfo")
    public ResponseData getGwbhInfo(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        int errcode = 200;
        String errmsg = "ok";
        List<Dto> list = new ArrayList<Dto>();
        try {
            list = gwat171Service.getGwbhInfo(inDto);
        } catch (Exception e) {
            errcode = 500;
            errmsg = "服务器出小差了";
            return new ErrorResponseData(errcode, errmsg);
        }
        return new SuccessResponseData(list);
    }


    /**
     * 提交资料  update
     *
     * @return
     */
    @RequestMapping("updateInfo")
    public ResponseData updateInfo(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        Date date = new Date();
        GwbhT gwbh_tPO = new GwbhT();
        int errcode = 200;
        String errmsg = "ok";
        try {

            gwbh_tPO.setGWBHENT(inDto.getString("GWBHENT"));
            gwbh_tPO.setGWBHNAME(inDto.getString("GWBHNAME"));
            gwbh_tPO.setGWBHID(inDto.getString("GWBHID"));
            gwbh_tPO.setGWBHSEQ(inDto.getBigDecimal("GWBHSEQ"));

            gwbh_tPO.setGWBH028(inDto.getString("GWBH028"));
            //gwbh_tPO.setGWBH030(inDto.getDate("GWBH030"));
            gwbh_tPO.setGWBH030(inDto.getDate("GWBH030").toLocalDate());
            gwbh_tPO.setGWBH032(inDto.getString("GWBH032"));

            gwbh_tPO.setGWBHMODU(currentUser.getID());
            gwbh_tPO.setGWBHTIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));

            UpdateWrapper<GwbhT> gwbhTUpdateWrapper = new UpdateWrapper<>();
            gwbhTUpdateWrapper.eq("GWBHENT", gwbh_tPO.getGWBHENT());
            gwbhTUpdateWrapper.eq("GWBHNAME", gwbh_tPO.getGWBHNAME());
            gwbhTUpdateWrapper.eq("GWBHID", gwbh_tPO.getGWBHID());
            gwbhTUpdateWrapper.eq("GWBHSEQ", gwbh_tPO.getGWBHSEQ());
            gwbh_tPO.update(gwbhTUpdateWrapper);

        } catch (Exception e) {
            e.printStackTrace();
            errcode = 500;
            errmsg = "服务器出小差了";
            return new ErrorResponseData(errcode, errmsg);
        }
        return new SuccessResponseData();
    }

    /**
     * 提交资料 添加领导批示
     *
     * @return
     * @author by wenchuan.li 2018/11/19 11:53
     */
    @RequestMapping("updateGwbh040")
    public ResponseData updateGwbh040(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        Date date = new Date();
        GwbhT gwbh_tPO = new GwbhT();
        int errcode = 200;
        String errmsg = "ok";
        try {

            gwbh_tPO.setGWBHENT(currentUser.getDBC());
            gwbh_tPO.setGWBHNAME(inDto.getString("GWBHNAME"));
            gwbh_tPO.setGWBHID(inDto.getString("GWBHID"));
            gwbh_tPO.setGWBHSEQ(inDto.getBigDecimal("GWBHSEQ"));
            gwbh_tPO.setGWBH040(inDto.getString("GWBH040"));
            gwbh_tPO.setGWBHMODU(currentUser.getID());
            gwbh_tPO.setGWBHTIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));

            UpdateWrapper<GwbhT> gwbhTUpdateWrapper = new UpdateWrapper<>();
            gwbhTUpdateWrapper.eq("GWBHENT", gwbh_tPO.getGWBHENT());
            gwbhTUpdateWrapper.eq("GWBHNAME", gwbh_tPO.getGWBHNAME());
            gwbhTUpdateWrapper.eq("GWBHID", gwbh_tPO.getGWBHID());
            gwbhTUpdateWrapper.eq("GWBHSEQ", gwbh_tPO.getGWBHSEQ());
            gwbh_tPO.update(gwbhTUpdateWrapper);

        } catch (Exception e) {
            e.printStackTrace();
            errcode = 500;
            errmsg = "服务器出小差了";
            return new ErrorResponseData(errcode, errmsg);
        }
        return new SuccessResponseData();
    }

}
